package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.WithdrawalRequest;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class WithdrawalRequestAdapter extends RecyclerView.Adapter<WithdrawalRequestAdapter.ViewHolder> {

    private List<WithdrawalRequest> requestList;
    private Context context;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(WithdrawalRequest request, int position);
    }

    public WithdrawalRequestAdapter(Context context, List<WithdrawalRequest> requestList, OnItemClickListener listener) {
        this.context = context;
        this.requestList = requestList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_withdrawal_request, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WithdrawalRequest request = requestList.get(position);
        
        // 绑定用户名称
        String displayName = request.getUserNickname();
        if (displayName == null || displayName.isEmpty()) {
            displayName = request.getUsername();
        }
        holder.tvUserName.setText(displayName);
        
        // 绑定金额
        holder.tvAmount.setText(String.format(Locale.getDefault(), "¥%.2f", request.getAmount()));
        
        // 绑定时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        holder.tvRequestTime.setText(sdf.format(request.getRequestTime()));
        
        // 绑定状态
        holder.tvStatus.setText(getStatusText(request.getStatus()));
        holder.tvStatus.setBackground(ContextCompat.getDrawable(context, getStatusBackground(request.getStatus())));
        
        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(request, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return requestList.size();
    }

    public void updateData(List<WithdrawalRequest> newList) {
        this.requestList = newList;
        notifyDataSetChanged();
    }

    private String getStatusText(String status) {
        if (WithdrawalRequest.STATUS_APPROVED.equals(status)) {
            return "已通过";
        } else if (WithdrawalRequest.STATUS_REJECTED.equals(status)) {
            return "已拒绝";
        } else {
            return "待处理";
        }
    }

    private int getStatusBackground(String status) {
        if (WithdrawalRequest.STATUS_APPROVED.equals(status)) {
            return R.drawable.tag_status_approved;
        } else if (WithdrawalRequest.STATUS_REJECTED.equals(status)) {
            return R.drawable.tag_status_rejected;
        } else {
            return R.drawable.tag_status_pending;
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvUserName, tvAmount, tvRequestTime, tvStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvUserName = itemView.findViewById(R.id.tv_user_name);
            tvAmount = itemView.findViewById(R.id.tv_amount);
            tvRequestTime = itemView.findViewById(R.id.tv_request_time);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }
    }
} 